﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using umbraco;
using umbraco.BasePages;
using umbraco.cms.presentation.Trees;

namespace CodeGecko.Umbraco.DataTypes.CustomTreeHandler
{
    public partial class DoctypeTreePicker : UmbracoEnsuredPage
    {
		protected void Page_Load(object sender, System.EventArgs e) { }

        protected string TreeInitUrl {
            get {
                return TreeService.GetInitUrl(null, TreeRequestParams.Instance.TreeType, false, true, 
                    TreeDialogModes.id, TreeRequestParams.Instance.Application, "", "").Replace("treeinit.aspx", "doctypetreeinit.aspx");
            }
        }
    }

    internal class TreeRequestParams : ITreeService {
        private TreeRequestParams() { }
        private static TreeRequestParams m_rp;
        /// <summary>
        /// Create a singleton that is stored in the current HttpContext
        /// </summary>
        public static TreeRequestParams Instance {
            get {
                if (!HttpContext.Current.Items.Contains("MyTreeRequestParameters")) HttpContext.Current.Items.Add("MyTreeRequestParameters", new TreeRequestParams());
                return HttpContext.Current.Items["MyTreeRequestParameters"] as TreeRequestParams;
            }
        }

        public string NodeKey {
            get {
                if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString["nodeKey"]))
                    return HttpContext.Current.Request.QueryString["nodeKey"];
                return "";
            }
        }

        public string Application {
            get {
                if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString["app"]))
                    return HttpContext.Current.Request.QueryString["app"];
                //appAlias is legacy... only here in case other scrips are using it
                if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString["appAlias"]))
                    return HttpContext.Current.Request.QueryString["appAlias"];
                return "";
            }
        }

        public int StartNodeID {
            get {
                int sNodeID;
                if (int.TryParse(HttpContext.Current.Request.QueryString["id"], out sNodeID))
                    return sNodeID;
                return -1;
            }
        }

        public string FunctionToCall {
            get {
                if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString["functionToCall"]))
                    return HttpContext.Current.Request.QueryString["functionToCall"];
                return "";
            }
        }

        public bool IsDialog {
            get {
                bool value;
                if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString["isDialog"]))
                    if (bool.TryParse(HttpContext.Current.Request.QueryString["isDialog"], out value))
                        return value;
                return false;
            }
        }

        public TreeDialogModes DialogMode {
            get {
                if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString["dialogMode"]) && IsDialog) {
                    try {
                        return (TreeDialogModes)Enum.Parse(typeof(TreeDialogModes), HttpContext.Current.Request.QueryString["dialogMode"]);
                    } catch {
                        return TreeDialogModes.none;
                    }
                }
                return TreeDialogModes.none;
            }
        }

        public bool ShowContextMenu {
            get {
                bool value;
                if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString["contextMenu"]))
                    if (bool.TryParse(HttpContext.Current.Request.QueryString["contextMenu"], out value))
                        return value;
                return true;
            }
        }

        public string TreeType {
            get {
                if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString["treeType"]))
                    return HttpContext.Current.Request.QueryString["treeType"];
                return "";
            }
        }
    }
}